#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const ll mx=3e5;
#define ff first
#define ss second
#define pi pair<ll,ll>
#define pii pair<pi,ll>
#define memo(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define MAX3(a,b,c) max(a,max(b,c))
#define MIN3(a,b,c) min(a,min(b,c))
#define V vector<ll>
#define VP vector<pi>
#define all(a) a.begin(),a.end()
#define PI acos(-1)
#define all(a) a.begin(),a.end()
#define check(a,b) ((1ll<<a)&b)
ll bigmod(ll b,ll p)
{
if(p==0)return 1;
if(p%2==0)
{
ll h=bigmod(b,p/2);
return (h*h)%mod;
}
else
{
return (b*(bigmod(b,p-1)))%mod;
}
}
void solve()
{
ll n,m;
cin>>n>>m;
map<ll,vector<ll>>xx;
ll a[n+10];
ll b[m+10];
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
ll p[n+10];
p[n+1]=1e18;
for(ll i=n;i>=1;i--)p[i]=min(a[i],p[i+1]);
for(ll i=1;i<=n;i++)
{
//if(p[i]==a[i])
xx[p[i]].pb(i);
//cout<<p[i]<<" ";
}
for(ll i=1;i<=m;i++)cin>>b[i];
for(ll i=1;i<=m;i++)
{
if(!xx[b[i]].size()||p[1]!=b[1])
{
cout<<0<<"\n";return;
}
}
ll ans=1;
for(ll i=2;i<=m;i++)
{
//cout<<xx[b[i]].back()<<" "<<xx[b[i-1]][0]<<"\n";
//if(xx[b[i]].size()==1)continue;
ans=(ans*(xx[b[i]].back()-xx[b[i]][0]+1))%mod;
}
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ll t;cin>>t;while(t--)
solve();
return 0;
}
1607A - Linear Keyboard | EQUALCOIN Equal Coins |
XOREQN Xor Equation | MAKEPAL Weird Palindrome Making |
HILLSEQ Hill Sequence | MAXBRIDGE Maximise the bridges |
WLDRPL Wildcard Replacement | 1221. Split a String in Balanced Strings |
1002. Find Common Characters | 1602A - Two Subsequences |
1555A - PizzaForces | 1607B - Odd Grasshopper |
1084A - The Fair Nut and Elevator | 1440B - Sum of Medians |
1032A - Kitchen Utensils | 1501B - Napoleon Cake |
1584B - Coloring Rectangles | 1562B - Scenes From a Memory |
1521A - Nastia and Nearly Good Numbers | 208. Implement Trie |
1605B - Reverse Sort | 1607C - Minimum Extraction |
1604B - XOR Specia-LIS-t | 1606B - Update Files |
1598B - Groups | 1602B - Divine Array |
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |